package com.schoolCommunity.controller;

import com.schoolCommunity.pojo.Commodity;
import com.schoolCommunity.pojo.Information;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class CommodityController {

    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL_PREFIX = "http://SCHOOLCOMMUNITY-COMMODITY";

    //负载均衡测试用
    @GetMapping("/CommodityHello")
    public String hello() {
        System.out.println("-------------------进入了测试--------------");
        return restTemplate.getForObject(REST_URL_PREFIX + "/hello", String.class);
    }

    //用户添加一个商品
    @PostMapping("/InsertCommodity")
    public Information InsertCommodity(@RequestBody Commodity commodity) throws IOException {
        return restTemplate.postForObject(REST_URL_PREFIX + "/InsertCommodity", commodity, Information.class);
    }

    //添加一张商品的照片
    @PostMapping("/UpdateCommodityPicture/{CommodityId}")
    public Information UpdateCommodityPicture(@RequestParam("file") MultipartFile file, @PathVariable("CommodityId") long CommodityId) throws IOException {
        return restTemplate.postForObject(REST_URL_PREFIX + "/UpdateCommodityPicture/" + CommodityId, file, Information.class);
    }

    //用户主动下架商品
    @GetMapping("/soldOutCommodity/{commodityId}")
    public Information soldOutCommodity(@PathVariable("commodityId") long commodityId) throws IOException {
        return restTemplate.getForObject(REST_URL_PREFIX + "/soldOutCommodity/" + commodityId, Information.class);
    }

    //获取某个用户的所有商品
    @GetMapping("/GetMyCommodity/{memberId}")
    public Information GetMyCommodity(@PathVariable("memberId") long memberId) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/GetMyCommodity/" + memberId, Information.class);
    }

    //分页获取所有商品列表，默认20页
    @GetMapping("/GetCommodityPage/{pageNo}")
    public Information GetCommodityPage(@PathVariable("pageNo") int PageNo) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/GetCommodityPage/" + PageNo, Information.class);
    }

    //获取最大分页
    @GetMapping("/GetMaxPageNo")
    public long GetMaxPageNo() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/GetMaxPageNo", long.class);
    }

    //用户购买商品
    @PostMapping("/BuyCommodity/{BuyMemberId}/{CommodityId}")
    public Information BuyCommodity(@PathVariable("BuyMemberId") long BuyMemberId, @PathVariable("CommodityId") long CommodityId) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/BuyCommodity/" + BuyMemberId + "/" + CommodityId, null, Information.class);
    }

    //管理员功能，删除某个产品
    @GetMapping("/DeleteCommodity/{commodityId}")
    public Information DeleteCommodity(@PathVariable("commodityId") long commodityId) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/DeleteCommodity/" + commodityId, Information.class);
    }

    //管理员功能，审核某个产品
    @GetMapping("/SuggestCommodity/{commodityId}/{suggestResult}")
    public Information SuggestCommodity(@PathVariable("commodityId") long commodityId, @PathVariable("suggestResult") int suggestResult) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/SuggestCommodity/" + commodityId + "/" + suggestResult, Information.class);
    }

    //管理员功能，分页获取尚未审核的商品
    @GetMapping("/GetNoSuggestCommodity/{pageNo}")
    public Information GetNoSuggestCommodity(@PathVariable("pageNo") long pageNo) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/GetNoSuggestCommodity/" + pageNo, Information.class);
    }

    //管理员功能，获取最大为分页商品页数
    @GetMapping("/GetNoSuggestCommodityMaxPage")
    public long GetNoSuggestCommodityMaxPage() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/GetNoSuggestCommodityMaxPage", long.class);
    }

    //根据购买人的id查询购买记录
    @GetMapping("/GetCommodityTransactionById/{memberId}")
    public Information GetCommodityTransactionById(@PathVariable("memberId") long memberId) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/GetCommodityTransactionById/" + memberId, Information.class);
    }


}
